<template>
  <div>
    <div class="search">
      <van-search v-model="value" show-action label="search" placeholder="请输入搜索关键词" @search="onSearch">
      </van-search>
      <van-icon @click="toPhone" class="searchicon" name="bars">
        <span @click="toPhone" class="searchicon2">分类</span>
      </van-icon>
    </div>
    <van-card v-for="(detailItem, index) in tableData" 
      :key="index"
      :price="detailItem.product.price" 
      :title="detailItem.product.title" 
      :desc="detailItem.product.description"
      :thumb="detailItem.timages[0].url" 
      @click="toDetails(detailItem.timages[0].productId)">
    </van-card>
    <br />
    <van-button class="buttonmore" type="primary" @click="expand">查看更多</van-button>
    <div style="height:100px"></div>
  </div>
</template>

<script>
let number = 0
export default {
  name: 'ProductPage',
  data() {
    return {
      value: '',
      tableData: [],
      totalData: [],
    };
  },

  components: {
  },

  created() {
    number = 4
    this.value = this.$store.state.sousuoinput
    this.axios({
      method: 'GET', url: "/aapi/select", params: {
        value: this.value
      }
    }).then((response) => {
      this.totalData = response.data
      this.tableData = response.data.slice(0, number)
    })
  },

  methods: {
    toPhone() {
      this.$router.push({
        path: '/laptop',
      })
    },
    expand() {
      number = number + 4
      this.tableData = this.totalData.slice(0, number)
    },
    toDetails(id) {
      this.$store.commit("changeproductid", id)
      this.$router.push({
        path: '/goodsdetail',
      })
    },
    onSearch() {
      number = 4
      this.axios({
        method: 'GET', url: "/aapi/select", params: {
          value: this.value
        }
      }).then((response) => {
        this.totalData = response.data
        this.tableData = response.data.slice(0, number)
      })
    },
  },
}
</script>
<style scoped>
.search {
  display: flex;
}

.searchicon {
  position: relative;
  top: 10px;
  left: 20px;
}

.searchicon2 {
  position: absolute;
  top: 20px;
  right: 1%;
  font-size: 12px;
  width: 25px;
}
.buttonmore{
  margin-top: 20px;
}
</style>